const constans = require('../../constans/constans.js');
const request = require('../../request.js');
const Toast = require('../../utils/Toast.js');
const LogUtils = require('../../utils/LogUtils.js');
const util = require('../../utils/util.js')
const app = getApp();
Page({
  data: {
    logs: [],
    height1:'',
    dip1:0,
    dis1_d:'block',
    user_image:'../../img/admin_linqu1.png',
    address: '',
    title: '',
    welcome:'',
    Count:'00',
    remainCount:'00',
    navTOgame:true,
    yemianboll:1,
    yue:0.00,
    opt:1,
    jiefu_package:null,
    timestamp1:null,
    in2:'',
  },
  onReady:function(){
  
   
  },
  onLoad: function (options) {

    Toast.loading("加载中...");
    
    console.log(wx.getStorageSync('qian')+'这个')
    this.setData({
      yue: (wx.getStorageSync('qian') / 100).toFixed(2),
      jiefu_package: options.p,
      timestamp1: options.timestamp,
    });
    console.log('这个是扫码进去的')
    console.log(options)
    // wx.setStorageSync('channel','scan');
    // wx.setStorageSync('activityId', options.a);
    if (options.a!=null){
      wx.setStorageSync('activityId', options.a);
    }else{

    }
    
    var b1 = options.c;
    var b2 = options.a;
    //如果是分享过来的界面就判断，赋值

    if (options.c!=undefined){
      wx.setStorageSync('channel', options.c)
    }
    if (options.invitation != undefined) {
      var inv_ = options.invitation
      this.setData({
        in2: inv_
      });
    }
    console.log('页面传值过来的的' + b1 + b2)
    let that = this;
    console.log('上面')
    console.log(options)
    let pan_bool=true;//判断是否抢到红包
    wx.login({
      success: res => {
        console.log(res)
        request.get(
          constans.minlogin,
          { code: res.code },
          function (data) {
            console.log('下面');
            console.log(data)
 
            console.log(data.data.data.session)
            wx.setStorageSync('session', data.data.data.session);
            if (data.data.data.user == null){
             
              console.log('空的')
              app.requestHeader = { 'session': data.data.data.session }
              that.setData({
                dip1: 1,
                dis1_d: 'block',
              });
              Toast.hideLoading();
            }else{
              wx.setStorageSync('invitation', data.data.data.user.id)
              console.log('不空的')
              app.requestHeader = { 'session': data.data.data.session }
              app.requestHeader = { 'Authorization': data.header.Authorization}
              wx.setStorageSync('Authorization', data.header.Authorization);
              console.log('用户' + wx.getStorageSync('Authorization'));
              console.log(app.requestHeader)
              wx.setStorageSync('nickname', data.data.data.user.nickname)
              wx.setStorageSync('avatar', data.data.data.user.avatar)
           
              request.get(
                constans.getActivity,
                { activityId: wx.getStorageSync('activityId'), channel: wx.getStorageSync('channel'), invitation:that.data.in2 },
                function (data) {
                  console.log('获取活动');
                  console.log(data)
                  Toast.hideLoading();
                  that.setData({
                    dis1_d: 'none',
                  });
                  if (data.data.code == 0) {
                    wx.setStorageSync('packageId', data.data.data.package.id);
                    wx.setStorageSync('Count', data.data.data.package.count)
                    wx.setStorageSync('remainCount', data.data.data.package.count - data.data.data.package.remainCount)
                    wx.setStorageSync('title', data.data.data.activity.extra.title)
                    that.setData({
                      address: data.data.data.activity.extra.address,
                      title: data.data.data.activity.extra.title,
                      welcome: data.data.data.activity.extra.welcome,
                      Count: data.data.data.package.count,
                      remainCount: data.data.data.package.count - data.data.data.package.remainCount,
                      user_image: data.data.data.activity.logo,

                    });
                    if (data.data.data.record!=null){
                      pan_bool=true;
                    }else{
                      pan_bool = false;
                      
                    }
                    if (data.data.data.package.remainCount == 0) {
                      that.setData({
                        navTOgame: false,
                      });
                      if (pan_bool){
                         
                      }else{
                        that.setData({
                          opt: 0.5,
                        });
                       
                      }
                      
                    }
            
                    if (data.data.data.record!=undefined){
                      that.setData({
                        yue: (data.data.data.record.amount / 100).toFixed(2),
                      });
                      if (data.data.data.record.amount != '') {
                        that.setData({
                          yemianboll: 0,
                        });

                      }
                    }
                    
                   
                    

                  } else {
                    // wx.showModal({
                    //   title: '提示',
                    //   content: data.message,
                    // })

                  }
                }
              )
            }
            
          }
          
        )
      }
    })




   
    // 获取系统信息
  
   

    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio;
        // 设置高度
        that.setData({
          height1: height
        });
      }
    });
    console.log(this.data.height1)
    //回调方法中实现具体逻辑
    console.log(app.requestHeader)
 

  },
  toGame:function(){
    let that=this;
    // 检查是否有可用红包
    request.get(
      constans.checkPackage,
      { activityId: wx.getStorageSync('activityId'), channel: wx.getStorageSync('channel'), packageId: that.data.jiefu_package, timestamp: that.data.timestamp1},
      function (data) {
        Toast.hideLoading();
        that.setData({
          dis1_d: 'none',
        });
        console.log('检查是够有可用红包');
        console.log(data)     //remainCount
        if (data.data.code == 0) {
          //  wx.clearStorageSync()
          wx.navigateTo({

            url: '../fGame/fGame',

          })

          // wx.setStorageSync('channel', 'scan');
          console.log('packageId' + wx.getStorageSync('packageId'));
          console.log('channel' + wx.getStorageSync('channel'));


        }
        else if (data.data.code == 30000) {
          that.setData({
            navTOgame: false,
          });
          // if (pan_bool) {

          // }else{
          //   that.setData({
          //     opt: 0.5,
          //   });
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: data.data.message,
          })
          // }
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: data.data.message,
          })
        }
      }
    )



 
    
     
   
  },
  toxiangqing:function(){
    wx.navigateTo({
      url: '../lingqu_jilu/lingqu_jilu'
    })
  },
  bindgetuserinfo: function (e) {
    console.log(e)
    let that=this
    console.log(JSON.parse(e.detail.rawData) )
    let user = JSON.parse(e.detail.rawData);
    console.log(user.nickName + user.avatarUrl + ';' + user.gender + ';' + user.city + ';' + user.country + ';'+ user.province)
    let msg = e.detail.errMsg;
  
    if (msg === 'getUserInfo:ok') {
    
      console.log(app.requestHeader)
      request.post(
        constans.register,
        { iv: e.detail.iv, encryptedData: e.detail.encryptedData, nickName: user.nickName, avatarUrl: user.avatarUrl, gender: user.gender, city: user.city, country: user.country, province: user.province, session: wx.getStorageSync('session')},
        function (data) {
          console.log('注册');
          console.log(data)
          if (data.data.code==0){
          
          
            wx.redirectTo({
                  url: '../packet/packet'
                })
           
         
            

              }else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: data.data.message,
            })
              
         }
        }
      )
    } else {
     
  
  }
  }, onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
    }
    request.get(
      constans.addShareRecord,
      { activityId: wx.getStorageSync('activityId'), packageId: wx.getStorageSync('packageId') },
      function (data) {

        if (data.data.code == 0) {
          console.log('分享成功')
          console.log(data)
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: data.data.message,
          })
        }
      })
    return {
      title: wx.getStorageSync('title')+"邀您翻牌子", 
      imageUrl:'../../img/Gran.jpg',
      path: "pages/packet/packet?c=share&a=" + wx.getStorageSync('activityId') + "&invitation=" + wx.getStorageSync('invitation') + "&timestamp=" + Date.parse(new Date()) + "&p=" + wx.getStorageSync('packageId')

    }
  },
   toMe: function () {
    wx.navigateTo({
      url: '../wode/wode'
    })
  },
  onShow(){
    let that=this;

  
            that.setData({
              Count: wx.getStorageSync('Count'),
              remainCount: wx.getStorageSync('remainCount'),
            });
     
  
  },


})

